package com.situ.userWeb.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.userWeb.Service.Impl.DepartmentServiceImpl;
import com.situ.userWeb.entity.Department;


@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{
	DepartmentServiceImpl departmentService = new DepartmentServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		if(method == null || "".equals(method)) {
			method = "selectAll";
		}
		switch(method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectId":
			selectId(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//		String id = req.getParameter("id");
//		String name = req.getParameter("name");
//		
//		Department department = new Department(Integer.parseInt(id), name);
//		boolean isSuccess = departmentService.update(department);
//		resp.sendRedirect(req.getContextPath() + "/department?method=selectAll");
	}

	private void selectId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		String id = req.getParameter("id");
//		
//		Department department = departmentService.selectId(Integer.parseInt(id));
//		
//		req.setAttribute("department", department);
//		req.getRequestDispatcher("/department_update.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//		String id = req.getParameter("id");
//		String name = req.getParameter("name");
//		Department department = new Department(Integer.parseInt(id), name);
//		
//		boolean isSuccess = departmentService.insert(department);
//		System.out.println(isSuccess);
//		resp.sendRedirect(req.getContextPath() + "/department?method=selectAll");
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//		String id = req.getParameter("id");
//		
//		boolean isSuccess = departmentService.delete(Integer.parseInt(id));
//		System.out.println(isSuccess);
//		
//		resp.sendRedirect(req.getContextPath() + "/department?method=selectAll");
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		List<Department> list = departmentService.selectAll();
//		
//		req.setAttribute("list", list);
//		req.getRequestDispatcher("department_list.jsp").forward(req, resp);
		
	}

}
